﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Maticsoft.BLL;
using Maticsoft.Model;
using FineUI;

namespace FineUI.Examples
{
    public partial class student_chose_course : System.Web.UI.Page
    {

        protected DataSet LoadCourseInform()
        {
            if(Session["UserName"]==null)
            {
              Response.Redirect("~/login.aspx");
            
            }

            string currentS_Id = Session["UserName"].ToString();
           

            DataSet DSStudentCourse = new DataSet();
            DataTable SelectCourseTable = new DataTable();

            SelectCourseTable.Columns.Add(new DataColumn("星期一", typeof(string)));
            SelectCourseTable.Columns.Add(new DataColumn("星期二", typeof(string)));
            SelectCourseTable.Columns.Add(new DataColumn("星期三", typeof(string)));
            SelectCourseTable.Columns.Add(new DataColumn("星期四", typeof(string)));
            SelectCourseTable.Columns.Add(new DataColumn("星期五", typeof(string)));
            SelectCourseTable.Columns.Add(new DataColumn("星期六", typeof(string)));
            SelectCourseTable.Columns.Add(new DataColumn("星期天", typeof(string)));

            for (int i = 1; i <= 11; i++)
            {
                DataRow currentRow = SelectCourseTable.NewRow();
                SelectCourseTable.Rows.Add(currentRow);
            }

            //添加表
            DSStudentCourse.Tables.Add(SelectCourseTable);

            Maticsoft.BLL.SelectCourse SelectCourse = new Maticsoft.BLL.SelectCourse();
            DataSet currentDataSet = SelectCourse.GetCourseInform(currentS_Id);


            //将信息写入表
            for (int i = 0; i < currentDataSet.Tables[0].Rows.Count; i++)
            {
                string currentTime=currentDataSet.Tables[0].Rows[i]["TC_Date_Time"].ToString();
                string currentWeek=currentDataSet.Tables[0].Rows[i]["TC_Date_Day"].ToString();
                string currentTeaClassName=currentDataSet.Tables[0].Rows[i]["TC_Name"].ToString();
                int index=0;
                try
                {
                    index=Convert.ToInt32(currentTime);
                }
                catch(Exception)
                {
                
                
                }

                if (index >=1)
                {
                    DSStudentCourse.Tables[0].Rows[index-1][currentWeek] = currentTeaClassName;
                }
               
            }

          //  if (!IsPostBack)//
          //  {
                DataView view1 = DSStudentCourse.Tables[0].DefaultView;
                Grid2.DataSource = view1;
                Grid2.DataBind();
            
          //  }
          

            return DSStudentCourse;
        
        
        }



        protected DataSet LoadTeachingClassInform()
        {
           
            Maticsoft.BLL.TeachingClass TeachingClass = new Maticsoft.BLL.TeachingClass();
            DataSet DSTeachingClass = TeachingClass.GetList("TeachingClass.T_id=Teacher.T_Id");
            return DSTeachingClass;
        }





        protected DataSet ShowTeachingClassInform()
        {
            Maticsoft.BLL.TeachingClass TeachingClass = new Maticsoft.BLL.TeachingClass();
            DataSet DSTeachingClass = TeachingClass.GetList("TeachingClass.T_id=Teacher.T_Id");
            for (int i = 0; i < DSTeachingClass.Tables[0].Rows.Count; i++)
            {
                DSTeachingClass.Tables[0].Rows[i]["TC_Date_Day"] = DSTeachingClass.Tables[0].Rows[i]["TC_Date_Day"] + "     第 " + DSTeachingClass.Tables[0].Rows[i]["TC_Date_Time"]+" 节课";
            }

            DataView view1 = DSTeachingClass.Tables[0].DefaultView;

            Grid1.DataSource = view1;
            Grid1.DataBind();
            return DSTeachingClass;


        }

        protected void ExistsPermiss(string P_Id, string P_Status)
        {
            Maticsoft.BLL.Permissions PM = new Maticsoft.BLL.Permissions();
            if (!PM.ExistsPermiss(P_Id, P_Status))
            {
                Response.Redirect("~/user_no_permission.aspx");
            }

        }


        protected void Page_Load(object sender, EventArgs e)
        {//获取当前用户的身份
            if (Session["UserName"]==null)
            {
                Response.Redirect("~/login.aspx");
            }
            //判断是否授权
            ExistsPermiss("S001", "已授权");
            ShowTeachingClassInform();
            LoadCourseInform();
        }

        protected void search_btn_Click(object sender, EventArgs e)
        {

        }

        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            ShowTeachingClassInform();
        }

        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e) 
        {
            if (e.CommandName == "ActionChose")//选课
            { 
                
                Maticsoft.BLL.SelectCourse SelectCourse=new Maticsoft.BLL.SelectCourse();
                Maticsoft.Model.SelectCourse SelectCourseInform = new Maticsoft.Model.SelectCourse();

                int key =e.RowIndex;
                DataSet CurrentData=LoadTeachingClassInform();
                string currentS_Id = Session["UserName"].ToString();

                string currentTC_Id = CurrentData.Tables[0].Rows[key]["TC_Id"].ToString();
                string currentTC_Date_Day=CurrentData.Tables[0].Rows[key]["TC_Date_Day"].ToString();
                string currentTC_Date_Time=CurrentData.Tables[0].Rows[key]["TC_Date_Time"].ToString();
                string currentTC_Name = CurrentData.Tables[0].Rows[key]["TC_Name"].ToString();


                if (SelectCourse.Exists(currentTC_Id, currentS_Id))
                {

                    Alert.ShowInTop("已经选择该课程", MessageBoxIcon.Information);
                    return;
                }
                if (SelectCourse.TimeIsFree(currentS_Id, currentTC_Date_Day, currentTC_Date_Time))
                {

                    Alert.ShowInTop("同一时间已经选择其他课程", MessageBoxIcon.Information);
                    return;
                }

                SelectCourseInform.S_Id = currentS_Id;
                SelectCourseInform.TC_Id = currentTC_Id;
                if (SelectCourse.Add(SelectCourseInform))
                {
                    DataSet currentCourseDataSet = LoadCourseInform();
                   /* int index = Convert.ToInt32(currentTC_Date_Time);
                    if (index >= 1)
                    {
                        currentCourseDataSet.Tables[0].Rows[index - 1][currentTC_Date_Day] = currentTC_Name;
                    }
                    
                    DataView view1 = currentCourseDataSet.Tables[0].DefaultView;
                    Grid2.DataSource = view1;
                    Grid2.DataBind();*/

                }
                else
                {

                    Alert.ShowInTop("选择课程失败", MessageBoxIcon.Information);
                    return;
                }

            
            
            }



            if (e.CommandName == "ActionCancel")//取消
            {
                Maticsoft.BLL.SelectCourse SelectCourse = new Maticsoft.BLL.SelectCourse();
                Maticsoft.Model.SelectCourse SelectCourseInform = new Maticsoft.Model.SelectCourse();

                int key = e.RowIndex;
                DataSet CurrentData = LoadTeachingClassInform();
                string currentS_Id = Session["UserName"].ToString();

                string currentTC_Id = CurrentData.Tables[0].Rows[key]["TC_Id"].ToString();
                

                if (SelectCourse.Delete(currentTC_Id, currentS_Id))
                {

                    LoadCourseInform();
                }


            }


        }
    }
}